<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;
use Illuminate\Support\Facades\DB;
// use Illuminate\Support\Facades\Model;
use App\Models\Cxhshop\Order;
class Income extends CxhshopBase {  

    public $timestamps = false;//这里表示的是不添加时间
    protected $table = '';
   
    
    //计算商品出售(合伙人只需要收取5%的平台维护费)  暂时考虑 单个商品 
    public function goods_sale($id,$orderid){
        $old_member = $this->get_relation_member($id);
       
        if(!$old_member){
            return '未获取到数据';
        }
        if(!$old_member['uex_parentList']){
            return '未获取到上级';
        }
        //添加规则 判断商品本身是否有比例设置的情况 
        $goods =$this->goods_specifications($orderid); 

        if(!$goods){
            return false;
        }
        
        
        //这里更改 商品销售-商品成本价格 来提成(判断混合支付情况)
        if($goods['can_mix']>0){
            $money = $goods['totalprice'] -$goods['costprice']*$goods['total']-$goods['dispatchprice'];
        }else{
            $money = $goods['price'] -$goods['costprice']*$goods['total'];
        }
       
        
        //获取比列参数
        $getparam = $this->getParameter();
        
        
        $id=substr($old_member['uex_parentList'],1,strlen($old_member['uex_parentList'])-2);
        
        //得到上级 会员 等等
        $member =$this-> get_tree_member($id);
        if(!$member){
            return '未获取到上级';
        }
        $param = explode(',', $id);
        $count = count($param);
        if($count<=1){
            return false;
        }
        //直推
        $record_id= $param[$count-2];
         //间推
        if($count<3){
            $record_id1 = '';
        }else{
            $record_id1 = $param[$count-3];
        }
       
        //购物积分比列
        $point = $getparam['shopping_points'];
        //余额比列
        $points = 1-($getparam['shopping_points']+$getparam['maintenance_fee']);
        $nowtime =time();
        $new_arr =array();
       
        try {
            DB::connection('jmft_cxh')->beginTransaction();
            foreach ($member as $key => $value) {
                //直推
                if($record_id&&$value['member_id']==$record_id){
                    //必须为合伙人
                    if($value['uex_level']>1){  

                        //满减不考虑(考虑商品自身提成)
                        //$basc = 0;
                        // foreach ($goods as $k => $v) {
                        // 	if($v['zhitui_balance']>0){
                        // 		$basc1 = $v['reality']*$v['zhitui_balance']*0.01;
                        // 	}else{
                        // 		$basc1 = $v['reality']*$getparam['good_zhitui'];
                                
                        // 	}
                        // 		$basc +=$basc1;
                        // }

                        if($goods['zhitui_balance']>0){
                            $basc = $money*$goods['zhitui_balance']*0.01;
                        }else{
                            $basc = $money*$getparam['good_zhitui'];
                        }
                        //购物积分
                        $res_point = $basc*$point;
                        //余额
                        $res_point1 = $basc*$points;
                        //维护费
                        $fee = $basc*$getparam['maintenance_fee'];
                        $condition1 = array('credit1'=>$res_point+$value['credit1'],'credit2'=>$res_point1+$value['credit2']);
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value['member_id'])->update($condition1);
                      
                        //back表
                        $log['apb_type'] = 6;
                        $log['bus_id'] = $orderid;
                        $log['apb_money'] = $res_point1;
                        $log['apb_fee'] = $fee;
                        $log['member_id'] = $value['member_id'];
                        $log['apb_intgral'] = $res_point;
                        $log['ctime'] = $nowtime;
                        $log['member_uuid'] = $value['member_uuid'];
                        $ids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log);
                       

                        //总流水记录
                        $tol_log['cash_bus_type'] = 6;
                        $tol_log['cash_type'] = 1;
                        $tol_log['cash_bus_id'] = $ids;
                        $tol_log['cash_now'] = $basc+$value['credit2'];
                        $tol_log['cash_old'] = $value['credit2'];
                        $tol_log['cash_num'] = '+'.$basc;
                        $tol_log['remark'] = '[商品销售提成]来自'.$old_member['cxhname'].' 返款余额' ;
                        $tol_log['member_id'] = $value['member_id'];
                        $tol_log['ctime'] =$nowtime;
                        $tol_log['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log);
                        

                        //平台费
                        $fee_log['cash_bus_type'] = 6;
                        $fee_log['cash_bus_id'] = $ids;
                        $fee_log['cash_type'] = 1;
                        $fee_log['cash_now'] = $value['credit2']+$basc-$fee;
                        $fee_log['cash_old'] = $value['credit2']+$basc;
                        $fee_log['cash_num'] = '-'.$fee;
                        $fee_log['remark'] = '[商品销售提成]来自'.$old_member['cxhname'] .' 扣除平台维护费' ;
                        $fee_log['member_id'] = $value['member_id'];
                        $fee_log['ctime'] =$nowtime;
                        $fee_log['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log);
                        
                        //扣除购物积分
                        $good_log['cash_bus_type'] = 6;
                        $good_log['cash_type'] = 1;
                        $good_log['cash_bus_id'] = $ids;
                        $good_log['cash_now'] = $value['credit2']+$basc-$fee-$res_point;
                        $good_log['cash_old'] = $value['credit2']+$basc-$fee;
                        $good_log['cash_num'] = '-'.$res_point;
                        $good_log['remark'] = '[商品销售提成]来自'.$old_member['cxhname'] .' 扣除购物积分';
                        $good_log['member_id'] = $value['member_id'];
                        $good_log['ctime'] =$nowtime;
                        $good_log['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log);
                        

                        //购物积分
                        $surplus_log['cash_bus_type'] = 6;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $ids;
                        $surplus_log['cash_now'] = $res_point+$value['credit1'];
                        $surplus_log['cash_old'] = $value['credit1'];
                        $surplus_log['cash_num'] = '+'.$res_point;
                        $surplus_log['remark'] = '[商品销售提成]来自'.$old_member['cxhname'] .' 发放购物积分';
                        $surplus_log['member_id'] = $value['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);
                      

                        if($value['uex_agent']>0){
                            $value['credit1'] = $value['credit1'] +$res_point;
                            $value['credit2'] = $res_point1+$value['credit2'];
                        }

                    }
                //间推
                }else if($value['member_id']==$record_id1&&$record_id1){
                    
                    //间推必须为高级
                    if($value['uex_level']=='3'){					
                        // $basc = 0;
                        // foreach ($goods as $k => $v) {

                        // 	if($v['jiantui_balance']){
                        // 		$basc1 = $v['reality']*$v['jiantui_balance']*0.01;
                        // 	}else{
                        // 		$basc1 = $v['reality']*$getparam['good_jiantui'];
                        // 	}
                        // 	$basc +=$basc1;
                        // }
                        
                        if($goods['jiantui_balance']>0){
                            $basc = $money*$goods['jiantui_balance']*0.01;
                        }else{
                            $basc = $money*$getparam['good_jiantui'];
                        }
                        
                        //维护费
                        $fee = $basc*$getparam['maintenance_fee'];
                        //购物积分
                        $goodspoint = $basc*$point;
                        $condition1 = array('credit1'=>$goodspoint+$value['credit1'],'credit2'=>$basc*$points+$value['credit2']);
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value['member_id'])->update($condition1);
                    
                        
                        //back流水
                        $log1['apb_type'] = 6;
                        $log1['bus_id'] = $orderid;
                        $log1['apb_money'] = $basc*$points;
                        $log1['apb_fee'] = $fee;
                        $log1['member_id'] = $value['member_id'];
                        $log1['apb_intgral'] = $goodspoint;
                        $log1['ctime'] =$nowtime;
                        $log1['member_uuid'] = $value['member_uuid'];
                        $ids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log1);
                       
                        //总收入
                        $tol_log1['cash_bus_type'] = 6;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $ids;
                        $tol_log1['cash_now'] = $basc+$value['credit2'];
                        $tol_log1['cash_old'] = $value['credit2'];
                        $tol_log1['cash_num'] = '+'.$basc;
                        $tol_log1['remark'] = '[商品销售提成]来自'.$old_member['cxhname'].' 返款余额';
                        $tol_log1['member_id'] = $value['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);
                    
                        //平台费
                        $fee_log1['cash_bus_type'] = 6;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $ids;
                        $fee_log1['cash_now'] = $value['credit2']+$basc-$fee;
                        $fee_log1['cash_old'] = $value['credit2']+$basc;
                        $fee_log1['cash_num'] = '-'.$fee;
                        $fee_log1['remark'] = '[商品销售提成]来自'.$old_member['cxhname'].' 扣除平台维护费';
                        $fee_log1['member_id'] = $value['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);

                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 6;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $ids;
                        $good_log1['cash_now'] = $value['credit2']+$basc-$fee-$goodspoint;
                        $good_log1['cash_old'] = $value['credit2']+$basc-$fee;
                        $good_log1['cash_num'] = '-'.$goodspoint;
                        $good_log1['remark'] = '[商品销售提成]来自'.$old_member['cxhname'] .' 扣除购物积分';
                        $good_log1['member_id'] = $value['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);
                       
                        // //剩余
                        $surplus_log1['cash_bus_type'] = 6;
                        $surplus_log1['cash_type'] = 2;
                        $surplus_log1['cash_bus_id'] = $ids;
                        $surplus_log1['cash_now'] = $value['credit1']+$goodspoint;
                        $surplus_log1['cash_old'] = $value['credit1'];
                        $surplus_log1['cash_num'] = '+'.$goodspoint;
                        $surplus_log1['remark'] = '[商品销售提成]来自'.$old_member['cxhname'] .' 发放购物积分';
                        $surplus_log1['member_id'] = $value['member_id'];
                        $surplus_log1['ctime'] =$nowtime;
                        $surplus_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log1);
            
                        if($value['uex_agent']>0){
                            $value['credit1'] = $value['credit1'] +$goodspoint;
                            $value['credit2'] = $basc*$points+$value['credit2'];
                        }

                    }

                }
                $new_arr =array();
                if($value['uex_agent']>0){
                    $new_arr[] = $value;

                }
            }
          
            //区域保护
            if($new_arr){
                $province = $city = $zone ='';
                    //设置参数
                $province = $getparam['goods_province'];
                $city = $getparam['goods_city'];
                $zone = $getparam['goods_zone'];

                //开始计算
                foreach ($new_arr as $key1 => $value1) {

                    if($value1['uex_agent']==3){
                        //升级的金额
                        // $basc_money=0;
                        // foreach ($goods as $k => $v) {

                        // 	if($v['province_balance']){
                        // 		$basc_money1 = $v['reality']*$v['province_balance']*0.01;
                        // 	}else{
                        // 		$basc_money1 = $v['reality']*$province;
                        // 	}
                        // 	$basc_money +=$basc_money1;

                        // }
                        if($goods['province_balance']>0){
                            $basc_money = $money*$goods['province_balance']*0.01;
                        }else{
                            $basc_money = $money*$getparam['goods_province'];
                        }
                        // $basc_money = $money*$province;
                        //重新查询数据 防止重复
                        //购物
                        $cxpoint = $basc_money*$point;
                        //余额
                        $sypoint = $basc_money*$points;
                        //维护费
                        $fee= $basc_money*$getparam['maintenance_fee'];
                        $p = array('credit1'=>$cxpoint+$value1['credit1'],'credit2'=>$sypoint+$value1['credit2']);
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value1['member_id'])->update($p);
                      
                        //直推流水
                        $log1['apb_type'] = 10;
                        $log1['bus_id'] = $orderid;
                        $log1['apb_money'] = $sypoint;
                        $log1['apb_fee'] = $fee;
                        $log1['member_id'] = $value1['member_id'];
                        $log1['apb_intgral'] =$cxpoint;
                        $log1['ctime'] =$nowtime;
                        $log1['member_uuid'] = $value['member_uuid'];
                        $ids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log1);
                        
                        //一总收入
                        $tol_log1['cash_bus_type'] = 10;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $ids;
                        $tol_log1['cash_now'] = $basc_money+$value1['credit2'];
                        $tol_log1['cash_old'] = $value1['credit2'];
                        $tol_log1['cash_num'] = '+'.$basc_money;
                        $tol_log1['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'].' 返款余额';
                        $tol_log1['member_id'] = $value1['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);

                        //平台费
                        $fee_log1['cash_bus_type'] = 10;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $ids;
                        $fee_log1['cash_now'] = $basc_money+$value1['credit2']-$fee;
                        $fee_log1['cash_old'] = $basc_money+$value1['credit2'];
                        $fee_log1['cash_num'] = '-'.$fee;
                        $fee_log1['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'] .' 扣除平台维护费' ;
                        $fee_log1['member_id'] = $value1['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);

                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 10;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $ids;
                        $good_log1['cash_now'] = $basc_money+$value1['credit2']-$fee-$cxpoint;
                        $good_log1['cash_old'] = $basc_money+$value1['credit2']-$fee;
                        $good_log1['cash_num'] = '-'.$cxpoint;
                        $good_log1['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'] .' 扣除购物积分';
                        $good_log1['member_id'] = $value1['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);

                        // //剩余
                        $surplus_log['cash_bus_type'] = 10;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $ids;
                        $surplus_log['cash_now'] = $cxpoint+$value1['credit1'];
                        $surplus_log['cash_old'] = $value1['credit1'];
                        $surplus_log['cash_num'] = '+'.$cxpoint;
                        $surplus_log['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'] .' 发放购物积分';
                        $surplus_log['member_id'] = $value1['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);

                    }else if($value1['uex_agent']==2){
                        // $basc_money  = 0;
                        // foreach ($goods as $k => $v) {
                        // 	if($v['city_balance']){
                        // 		$basc_money1 = $v['reality']*$v['city_balance']*0.01;
                        // 	}else{
                        // 		$basc_money1 = $v['reality']*$city;
                        // 	}
                        // 	$basc_money +=$basc_money1;
                        // }
                        if($goods['city_balance']>0){
                            $basc_money = $money*$goods['city_balance']*0.01;
                        }else{
                            $basc_money = $money*$getparam['goods_city'];
                        }

                        $basc_money = $money*$city;
                        //购物
                        $cxpoint = $basc_money*$point;
                        //余额
                        $sypoint = $basc_money*$points;
                        //维护费
                        $fee= $basc_money*$getparam['maintenance_fee'];

                        $p = array('credit1'=>$cxpoint+$value1['credit1'],'credit2'=>$sypoint+$value1['credit2']);
                       
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value1['member_id'])->update($p);
                        //直推流水
                        $log1['apb_type'] = 10;
                        $log1['bus_id'] = $orderid;
                        $log1['apb_money'] = $sypoint;
                        $log1['apb_fee'] = $fee;
                        $log1['member_id'] = $value1['member_id'];
                        $log1['apb_intgral'] = $cxpoint;
                        $log1['ctime'] =$nowtime;
                        $log1['member_uuid'] = $value['member_uuid'];
                        $ids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log1);
                       

                        //一总收入
                        $tol_log1['cash_bus_type'] = 10;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $ids;
                        $tol_log1['cash_now'] = $basc_money+$value1['credit2'];
                        $tol_log1['cash_old'] = $value1['credit2'];
                        $tol_log1['cash_num'] = '+'.$basc_money;
                        $tol_log1['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'].' 返款余额' ;
                        $tol_log1['member_id'] = $value1['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);
                        

                        //平台费
                        $fee_log1['cash_bus_type'] = 10;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $ids;
                        $fee_log1['cash_now'] = $basc_money+$value1['credit2']-$fee;
                        $fee_log1['cash_old'] = $basc_money+$value1['credit2'];
                        $fee_log1['cash_num'] = '-'.$fee;
                        $fee_log1['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'].' 扣除平台维护费' ;
                        $fee_log1['member_id'] = $value1['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);
                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 10;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $ids;
                        $good_log1['cash_now'] = $basc_money+$value1['credit2']-$fee-$basc_money*$point;
                        $good_log1['cash_old'] = $basc_money+$value1['credit2']-$fee;
                        $good_log1['cash_num'] = '-'.$basc_money*$point;
                        $good_log1['remark'] = '[代理商商品区域保护]来自'.$value1['cxhname'] .' 扣除购物积分';
                        $good_log1['member_id'] = $value1['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);

                        // //剩余
                        $surplus_log['cash_bus_type'] = 10;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $ids;
                        $surplus_log['cash_now'] = $cxpoint+$value1['credit1'];
                        $surplus_log['cash_old'] = $value1['credit1'];
                        $surplus_log['cash_num'] = '+'.$cxpoint;
                        $surplus_log['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'].' 发放购物积分';
                        $surplus_log['member_id'] = $value1['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);
                        

                    }else if($value1['uex_agent']==1){
                        
                        // $basc_money = 0;
                        // foreach ($goods as $k => $v) {
                        // 	if($v['area_balance']){
                        // 		$basc_money1 = $v['reality']*$v['area_balance']*0.01;
                        // 	}else{
                        // 		$basc_money1 = $v['reality']*$zone;
                        // 	}
                        // 	$basc_money +=$basc_money1;

                        // }
                        if($goods['area_balance']>0){
                            $basc_money = $money*$goods['area_balance']*0.01;
                        }else{
                            $basc_money = $money*$getparam['goods_zone'];
                        }
                        $basc_money = $money*$zone;
                        $cxpoint = $basc_money*$point;
                        $sypoint = $basc_money*$points;
                        $fee= $basc_money*$getparam['maintenance_fee'];
                        $p = array('credit1'=>$cxpoint+$value1['credit1'],'credit2'=>$sypoint+$value1['credit2']);
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value1['member_id'])->update($p);


                        //间推流水
                        $log1['apb_type'] = 10;
                        $log1['bus_id'] = $orderid;
                        $log1['apb_money'] = $sypoint;
                        $log1['apb_fee'] = $fee;
                        $log1['member_id'] = $value1['member_id'];
                        $log1['apb_intgral'] = $cxpoint;
                        $log1['ctime'] =$nowtime;
                        $log1['member_uuid'] = $value['member_uuid'];
                        $ids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log1);
                        
                        //一总收入
                        $tol_log1['cash_bus_type'] = 10;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $ids;
                        $tol_log1['cash_now'] = $basc_money+$value1['credit2'];
                        $tol_log1['cash_old'] = $value1['credit2'];
                        $tol_log1['cash_num'] = '+'.$basc_money;
                        $tol_log1['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'].' 返款余额';
                        $tol_log1['member_id'] = $value1['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);

                        //平台费
                        $fee_log1['cash_bus_type'] = 10;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $ids;
                        $fee_log1['cash_now'] = $basc_money+$value1['credit2']-$fee;
                        $fee_log1['cash_old'] = $basc_money+$value1['credit2'];
                        $fee_log1['cash_num'] = '-'.$fee;
                        $fee_log1['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'] .' 扣除平台维护费' ;
                        $fee_log1['member_id'] = $value1['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);
                      

                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 10;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $ids;
                        $good_log1['cash_now'] = $basc_money+$value1['credit2']-$fee-$basc_money*$point;
                        $good_log1['cash_old'] = $basc_money+$value1['credit2']-$fee;
                        $good_log1['cash_num'] = '-'.$basc_money*$point;
                        $good_log1['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'] .' 扣除购物积分';
                        $good_log1['member_id'] = $value1['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);
                        
                        //剩余
                        $surplus_log['cash_bus_type'] = 10;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $ids;
                        $surplus_log['cash_now'] = $cxpoint+$value1['credit1'];
                        $surplus_log['cash_old'] = $value1['credit1'];
                        $surplus_log['cash_num'] = '+'.$cxpoint;
                        $surplus_log['remark'] = '[代理商商品区域保护]来自'.$old_member['cxhname'] .' 发放购物积分';
                        $surplus_log['member_id'] = $value1['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] = $value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);
                    }
                }
            }
            if(in_array('',$res) || in_array(0, $res) || in_array(false, $res)){
                DB::connection('jmft_cxh')->rollBack();
                return false;
            }else{
                DB::connection('jmft_cxh')->commit();
                return true;
            }

        } catch (Exception $e) { 
            DB::connection('jmft_cxh')->rollBack();
            return false;
        }
    }


    
//计算会员礼包
    public function membership_gift_bag($id,$money){
        
        $old_member = $this->get_relation_member($id);
        if(!$old_member){
            return '未获取到数据';
        }
        if(!$old_member['uex_parentList']){
            return '未获取到上级';
        }
        //获取比列参数
        $getparam =$this->getParameter();
       
        $id=substr($old_member['uex_parentList'],1,strlen($old_member['uex_parentList'])-2);
        //得到上级 会员 等等
        $member =$this->get_tree_member($id);
       
        $param = explode(',', $id);
        $count = count($param);
        //直推
        $record_id= $param[$count-2];
        if($count<3){
            $record_id1 ='';
        }else{
            //间推
            $record_id1 = $param[$count-3];
        }
        //购物积分
        $point = $getparam['shopping_points'];
        //余额比列
        $points = 1-($getparam['shopping_points']+$getparam['maintenance_fee']);
        $nowtime =time();
        $new_arr =array();
        $res1 =1;

        try {
            DB::connection('jmft_cxh')->beginTransaction();
            foreach ($member as &$value) {

            //直推
                if($record_id&&$value['member_id']==$record_id){
                    //必须为合伙人
                    if($value['uex_level']>1){
                        //直推属性
                        //升级的金额
                        $basc = $money*$getparam['direct_reward'];
                        $res_point = $basc*$point;
                        $res_point1 = $basc*$points;
                        $condition1 = array('credit1'=>$res_point+$value['credit1'],'credit2'=>$res_point1+$value['credit2']);
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value['member_id'])->update($condition1);
                        

                        //back表流水
                        $log['apb_type'] = 4;
                        $log['bus_id'] = 0;
                        $log['apb_money'] = $res_point1;
                        $log['apb_fee'] = $basc*$getparam['maintenance_fee'];
                        $log['member_id'] = $value['member_id'];
                        $log['apb_intgral'] = $res_point;
                        $log['ctime'] =$nowtime;
                        $log['member_uuid'] =$value['member_uuid'];
                        $ids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log);
                        
                        //现金流水
                        $tol_log['cash_bus_type'] = 4;
                        $tol_log['cash_type'] = 1;
                        $tol_log['cash_bus_id'] = $ids;
                        $tol_log['cash_now'] = $basc+$value['credit2'];
                        $tol_log['cash_old'] = $value['credit2'];
                        $tol_log['cash_num'] = '+'.$basc;
                        $tol_log['remark'] = '[会员个人礼包]来自'.$old_member['cxhname'].' 返款余额';
                        $tol_log['member_id'] = $value['member_id'];
                        $tol_log['ctime'] =$nowtime;
                        $tol_log['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log);

                        //平台费
                        $fee_log['cash_bus_type'] = 4;
                        $fee_log['cash_type'] = 1;
                        $fee_log['cash_bus_id'] = $ids;
                        $fee_log['cash_now'] = $value['credit2']+$basc-$basc*$getparam['maintenance_fee'];
                        $fee_log['cash_old'] = $value['credit2']+$basc;
                        $fee_log['cash_num'] = '-'.$basc*$getparam['maintenance_fee'];
                        $fee_log['remark'] = '[会员个人礼包]来自'.$old_member['cxhname'].' 扣除平台维护费';
                        $fee_log['member_id'] = $value['member_id'];
                        $fee_log['ctime'] =$nowtime;
                        $fee_log['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log);
                        //扣除购物积分
                        $good_log['cash_bus_type'] = 4;
                        $good_log['cash_type'] = 1;
                        $good_log['cash_bus_id'] = $ids;
                        $good_log['cash_now'] = $value['credit2']+$basc-$basc*$getparam['maintenance_fee']-$res_point;
                        $good_log['cash_old'] = $value['credit2']+$basc-$basc*$getparam['maintenance_fee'];
                        $good_log['cash_num'] = '-'.$res_point;
                        $good_log['remark'] = '[会员个人礼包]来自'.$old_member['cxhname'].' 扣除购物积分';
                        $good_log['member_id'] = $value['member_id'];
                        $good_log['ctime'] =$nowtime;
                        $good_log['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log);
                        //剩余

                        $surplus_log['cash_bus_type'] = 4;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $ids;
                        $surplus_log['cash_now'] = $res_point+$value['credit1'];
                        $surplus_log['cash_old'] = $value['credit1'];
                        $surplus_log['cash_num'] = '+'.$res_point;
                        $surplus_log['remark'] = '[会员个人礼包]来自'.$old_member['cxhname'].' 发放购物积分';
                        $surplus_log['member_id'] = $value['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);

                        if($value['uex_agent']>0){
                            $value['credit1'] = $value['credit1'] +$res_point;
                            $value['credit2'] = $res_point1+$value['credit2'];
                        }

                    }
                //间推

                }else if($value['member_id']==$record_id1&&$record_id1){
                    //间推必须为高级
                    if($value['uex_level']=='3'){
                        $basc1 = $money*$getparam['Inter_reward'];
                        //维护费
                        $kou_point = $basc1*$getparam['maintenance_fee'];
                        $condition3 = array('credit1'=>$basc1*$point+$value['credit1'],'credit2'=>$basc1*$points+$value['credit2']);
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value['member_id'])->update($condition3);
                        
                        //间推流水
                        $log1['apb_type'] = 4;
                        $log1['bus_id'] = 0;
                        $log1['apb_money'] = $basc1*$points;
                        $log1['apb_fee'] = $kou_point;
                        $log1['member_id'] = $value['member_id'];
                        $log1['apb_intgral'] = $basc1*$point;
                        $log1['ctime'] =$nowtime;
                        $log1['member_uuid'] =$value['member_uuid'];
                        $ids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log1);
                       
                        //总收入
                        $tol_log1['cash_bus_type'] = 4;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $ids;
                        $tol_log1['cash_now'] = $basc1+$value['credit2'];
                        $tol_log1['cash_old'] = $value['credit2'];
                        $tol_log1['cash_num'] = '+'.$basc1;
                        $tol_log1['remark'] = '[会员个人礼包]来自'.$old_member['cxhname'].' 返款余额';
                        $tol_log1['member_id'] = $value['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);

                        //平台费
                        $fee_log1['cash_bus_type'] = 4;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $ids;
                        $fee_log1['cash_now'] = $value['credit2']+$basc1-$kou_point;
                        $fee_log1['cash_old'] = $value['credit2']+$basc1;
                        $fee_log1['cash_num'] = '-'.$kou_point;
                        $fee_log1['remark'] = '[会员个人礼包]来自'.$old_member['cxhname'].' 扣除平台维护费';
                        $fee_log1['member_id'] = $value['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);
                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 4;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $ids;
                        $good_log1['cash_now'] = $value['credit2']+$basc1-$kou_point-$basc1*$point;
                        $good_log1['cash_old'] = $value['credit2']+$basc1-$kou_point;
                        $good_log1['cash_num'] = '-'.$basc1*$point;
                        $good_log1['remark'] = '[会员个人礼包]来自'.$old_member['cxhname'].'  扣除购物积分';
                        $good_log1['member_id'] = $value['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);
                        //剩余
                        $surplus_log1['cash_bus_type'] = 4;
                        $surplus_log1['cash_type'] = 2;
                        $surplus_log1['cash_bus_id'] = $ids;
                        $surplus_log1['cash_now'] = $basc1*$point+$value['credit1'];
                        $surplus_log1['cash_old'] = $value['credit1'];
                        $surplus_log1['cash_num'] = '+'.$basc1*$point;
                        $surplus_log1['remark'] = '[会员个人礼包]来自'.$old_member['cxhname'] .' 发放购物积分';
                        $surplus_log1['member_id'] = $value['member_id'];
                        $surplus_log1['ctime'] =$nowtime;
                        $surplus_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log1);

                        if($value['uex_agent']>0){
                            $value['credit1'] = $basc1*$point+$value['credit1'];
                            $value['credit2'] = $basc1*$points+$value['credit2'];
                        }
                    }

                }

                if($value['uex_agent']>0){
                    $new_arr[] = $value;
                }
            }

            if($new_arr){
                $province = $city = $zone ='';
                $newarr=sortArray($new_arr,'uex_agent',SORT_ASC);
                //三个的情况
                if($newarr['0']['uex_agent']==1){
                    //设置参数
                    $province = $getparam['all_province_gradation'];
                    $city = $getparam['all_city_gradation'];
                    $zone = $getparam['all_zone_gradation'];
                }else if($newarr['0']['uex_agent']==2){
                    $province = $getparam['two_province_gradation'];
                    $city = $getparam['two_city_gradation'];
                //只有省
                }else{
                    $province = $getparam['single_province_gradation'];
                }

                //开始计算  (包括报单奖励)
                foreach ($newarr as $key1 => $value1) {
                    if($value1['uex_agent']==3){
                        //升级的金额
                        $basc_money= $money*$province;
                        $gift = $money * $getparam['gift_order'];
                        //重新查询数据 防止重复
                        $cxpoint = $basc_money*$point;
                        $sypoint = $basc_money*$points;
                        $gifts = $gift*$points;
                        $gift_point = $gift*$point;
                        $p = array('credit1'=>$cxpoint+$gift_point+$value1['credit1'],'credit2'=>$sypoint+$value1['credit2']+$gifts);
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value1['member_id'])->update($p);
                        

                        $fee= $basc_money*$getparam['maintenance_fee'];
                        $gift_fee = $gift*$getparam['maintenance_fee'];
                        //back表级差流水
                        $log1['apb_type'] = 8;
                        $log1['bus_id'] = 0;
                        $log1['apb_money'] = $sypoint;
                        $log1['apb_fee'] = $fee;
                        $log1['member_id'] = $value1['member_id'];
                        $log1['apb_intgral'] =$cxpoint;
                        $log1['ctime'] =$nowtime;
                        $log1['member_uuid'] =$value['member_uuid'];
                        $aids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log1);
                        
                        //保单奖励流水
                        $log2['apb_type'] = 9;
                        $log2['bus_id'] = 0;
                        $log2['apb_money'] =$gifts;
                        $log2['apb_fee'] = $gift_fee;
                        $log2['member_id'] = $value1['member_id'];
                        $log2['apb_intgral'] =$gift_point;
                        $log2['ctime'] =$nowtime;
                        $log2['member_uuid'] =$value['member_uuid'];
                        $bids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log2);
                       
                        //log 表级差流水
                        $tol_log1['cash_bus_type'] = 8;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $aids;
                        $tol_log1['cash_now'] = $basc_money+$value1['credit2'];
                        $tol_log1['cash_old'] = $value1['credit2'];
                        $tol_log1['cash_num'] = '+'.$basc_money;
                        $tol_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'].' 返款余额';
                        $tol_log1['member_id'] = $value1['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);

                        //平台费
                        $fee_log1['cash_bus_type'] = 8;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $aids;
                        $fee_log1['cash_now'] = $basc_money+$value1['credit2']-$fee;
                        $fee_log1['cash_old'] = $basc_money+$value1['credit2'];
                        $fee_log1['cash_num'] = '-'.$fee;
                        $fee_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'] .' 扣除平台维护费';
                        $fee_log1['member_id'] = $value1['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);

                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 8;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $aids;
                        $good_log1['cash_now'] = $basc_money+$value1['credit2']-$fee-$basc_money*$point;
                        $good_log1['cash_old'] = $basc_money+$value1['credit2']-$fee;
                        $good_log1['cash_num'] = '-'.$basc_money*$point;
                        $good_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'] .' 扣除购物积分';
                        $good_log1['member_id'] = $value1['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);

                        //剩余
                        $surplus_log['cash_bus_type'] = 8;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $aids;
                        $surplus_log['cash_now'] = $cxpoint+$value1['credit1'];
                        $surplus_log['cash_old'] = $value1['credit1'];
                        $surplus_log['cash_num'] = '+'.$cxpoint;
                        $surplus_log['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'] .' 发放购物积分';
                        $surplus_log['member_id'] = $value1['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);

                        //log 表保单流水
                        $tol_log1['cash_bus_type'] = 9;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $bids;
                        $tol_log1['cash_now'] = $value1['credit2']+$gift+$sypoint;
                        $tol_log1['cash_old'] = $value1['credit2']+$sypoint;
                        $tol_log1['cash_num'] = '+'.$gift;
                        $tol_log1['remark'] = '[代理商保单奖励]来自'.$old_member['cxhname'].' 返款余额' ;
                        $tol_log1['member_id'] = $value1['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);

                        //平台费
                        $fee_log1['cash_bus_type'] = 9;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $bids;
                        $fee_log1['cash_now'] = $value1['credit2']+$gift+$sypoint-$gift_fee;
                        $fee_log1['cash_old'] = $value1['credit2']+$gift+$sypoint;
                        $fee_log1['cash_num'] = '-'.$gift_fee;
                        $fee_log1['remark'] = '[代理商保单奖励]来自'.$old_member['cxhname'].' 扣除平台维护费';
                        $fee_log1['member_id'] = $value1['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);

                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 9;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $bids;
                        $good_log1['cash_now'] = $value1['credit2']+$gift+$sypoint-$gift_fee-$gift_point;
                        $good_log1['cash_old'] = $value1['credit2']+$gift+$sypoint-$gift_fee;
                        $good_log1['cash_num'] = '-'.$gift_point;
                        $good_log1['remark'] = '[代理商保单奖励]来自'.$old_member['cxhname'] .' 扣除购物积分' ;
                        $good_log1['member_id'] = $value1['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);

                        //剩余
                        $surplus_log['cash_bus_type'] = 9;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $bids;
                        $surplus_log['cash_now'] = $value1['credit1']+$gift_point+$cxpoint;
                        $surplus_log['cash_old'] = $value1['credit1']+$cxpoint;
                        $surplus_log['cash_num'] = '+'.$gift_point;
                        $surplus_log['remark'] = '[代理商保单奖励]来自'.$old_member['cxhname'] .' 发放购物积分' ;
                        $surplus_log['member_id'] = $value1['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);

                    }else if($value1['uex_agent']==2){
                        $basc_money = $money*$city;
                        $cxpoint = $basc_money*$point;
                        $sypoint = $basc_money*$points;
                        $p = array('credit1'=>$cxpoint+$value1['credit1'],'credit2'=>$sypoint+$value1['credit2']);
                       
                
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value1['member_id'])->update($p);
                        $fee= $basc_money*$getparam['maintenance_fee'];
                        //间推流水
                        $log1['apb_type'] = 8;
                        $log1['bus_id'] = 0;
                        $log1['apb_money'] = $sypoint;
                        $log1['apb_fee'] = $fee;
                        $log1['member_id'] = $value1['member_id'];
                        $log1['apb_intgral'] = $cxpoint;
                        $log1['ctime'] =$nowtime;
                        $log1['member_uuid'] =$value['member_uuid'];
                        $aids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log1);
                        
                        //一总收入
                        $tol_log1['cash_bus_type'] = 8;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $aids;
                        $tol_log1['cash_now'] = $basc_money+$value1['credit2'];
                        $tol_log1['cash_old'] = $value1['credit2'];
                        $tol_log1['cash_num'] = '+'.$basc_money;
                        $tol_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'].' 返款余额';
                        $tol_log1['member_id'] = $value1['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);

                        //平台费
                        $fee_log1['cash_bus_type'] = 8;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $aids;
                        $fee_log1['cash_now'] = $basc_money+$value1['credit2']-$fee;
                        $fee_log1['cash_old'] = $basc_money+$value1['credit2'];
                        $fee_log1['cash_num'] = '-'.$fee;
                        $fee_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'] .' 扣除平台维护费' ;
                        $fee_log1['member_id'] = $value1['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);

                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 8;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $aids;
                        $good_log1['cash_now'] = $basc_money+$value1['credit2']-$fee-$basc_money*$point;
                        $good_log1['cash_old'] = $basc_money+$value1['credit2']-$fee;
                        $good_log1['cash_num'] = '-'.$basc_money*$point;
                        $good_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'].' 扣除购物积分';
                        $good_log1['member_id'] = $value1['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);

                        //剩余
                        $surplus_log['cash_bus_type'] = 8;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $aids;
                        $surplus_log['cash_now'] = $value1['credit1'];
                        $surplus_log['cash_old'] = $cxpoint+$value1['credit1'];
                        $surplus_log['cash_num'] = '+'.$cxpoint;
                        $surplus_log['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'].' 发放购物积分';
                        $surplus_log['member_id'] = $value1['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);

                    }else if($value1['uex_agent']==1){
                        $basc_money = $money*$zone;
                        $cxpoint = $basc_money*$point;
                        $sypoint = $basc_money*$points;
                        $p = array('credit1'=>$cxpoint+$value1['credit1'],'credit2'=>$sypoint+$value1['credit2']);
                        $res[] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member')->where('id','=',$value1['member_id'])->update($p);
                        $fee= $basc_money*$getparam['maintenance_fee'];
                        //间推流水
                        $log1['apb_type'] = 8;
                        $log1['bus_id'] = 0;
                        $log1['apb_money'] = $sypoint;
                        $log1['apb_fee'] = $fee;
                        $log1['member_id'] = $value1['member_id'];
                        $log1['apb_intgral'] = $cxpoint;
                        $log1['ctime'] =$nowtime;
                        $log1['member_uuid'] =$value['member_uuid'];
                        $aids= DB::connection('jmft_cxh')->table('ims_cxh_apply_subsidy_back')->insertGetId($log1);
                       
                        //一总收入
                        $tol_log1['cash_bus_type'] = 8;
                        $tol_log1['cash_type'] = 1;
                        $tol_log1['cash_bus_id'] = $aids;
                        $tol_log1['cash_now'] = $basc_money+$value1['credit2'];
                        $tol_log1['cash_old'] = $value1['credit2'];
                        $tol_log1['cash_num'] = '+'.$basc_money;
                        $tol_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'].' 返款余额' ;
                        $tol_log1['member_id'] = $value1['member_id'];
                        $tol_log1['ctime'] =$nowtime;
                        $tol_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($tol_log1);

                        //平台费
                        $fee_log1['cash_bus_type'] = 8;
                        $fee_log1['cash_type'] = 1;
                        $fee_log1['cash_bus_id'] = $aids;
                        $fee_log1['cash_now'] = $basc_money+$value1['credit2']-$fee;
                        $fee_log1['cash_old'] = $basc_money+$value1['credit2'];
                        $fee_log1['cash_num'] = '-'.$fee;
                        $fee_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'].' 扣除平台维护费';
                        $fee_log1['member_id'] = $value1['member_id'];
                        $fee_log1['ctime'] =$nowtime;
                        $fee_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($fee_log1);

                        //扣除购物积分
                        $good_log1['cash_bus_type'] = 8;
                        $good_log1['cash_type'] = 1;
                        $good_log1['cash_bus_id'] = $aids;
                        $good_log1['cash_now'] = $basc_money+$value1['credit2']-$fee-$basc_money*$point;
                        $good_log1['cash_old'] = $basc_money+$value1['credit2']-$fee;
                        $good_log1['cash_num'] = '-'.$basc_money*$point;
                        $good_log1['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'] .' 扣除购物积分';
                        $good_log1['member_id'] = $value1['member_id'];
                        $good_log1['ctime'] =$nowtime;
                        $good_log1['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($good_log1);

                        // //剩余
                        $surplus_log['cash_bus_type'] = 8;
                        $surplus_log['cash_type'] = 2;
                        $surplus_log['cash_bus_id'] = $aids;
                        $surplus_log['cash_now'] = $value1['credit1'];
                        $surplus_log['cash_old'] = $value1['credit1']+$cxpoint;
                        $surplus_log['cash_num'] = '+'.$cxpoint;
                        $surplus_log['remark'] = '[代理商会员礼包]来自'.$old_member['cxhname'] .' 发放购物积分';
                        $surplus_log['member_id'] = $value1['member_id'];
                        $surplus_log['ctime'] =$nowtime;
                        $surplus_log['member_uuid'] =$value['member_uuid'];
                        $res[]= DB::connection('jmft_cxh')->table('ims_cxh_cash_log')->insert($surplus_log);
                    }
                }

            }

            if(in_array('',$res) || in_array('0', $res) || in_array('false', $res)){
                DB::connection('jmft_cxh')->rollBack();
                
                return false;
            }else{
                DB::connection('jmft_cxh')->commit();
                return true;
            }
        } catch (Exception $e) {
            
            DB::connection('jmft_cxh')->rollBack();
            return '失败';
        }
    }

    //当前会员从属关系
    private function get_relation_member($id,$field=['u.id','p.uex_parentList','u.cxhname'])
    {
        $data = DB::connection('jmft_cxh')->table('ims_ewei_shop_member as u')->join('ims_cxh_user_ex as p', 'u.id', '=', 'p.member_id')
        ->where('u.id','=',$id)->first($field);
        if(!empty($data)){
            return object_array($data);
        }else{
            return false;
        }
    }
    //订单相关的 商品 信息
    private function goods_specifications($orderid){
        $field =[
            'o.price','o.reality','o.costprice','o.total','g.zhitui_balance','g.jiantui_balance','g.area_balance',
            'g.city_balance',
            'g.province_balance',
            'r.can_mix',
            'r.price as totalprice',
            'r.dispatchprice'
        ];
        $data = DB::connection('jmft_cxh')->table('ims_ewei_shop_order_goods as o')
        ->join('ims_ewei_shop_goods as g', 'o.goodsid', '=', 'g.id')
        ->join('ims_ewei_shop_order as r', 'r.id', '=', 'o.orderid')
        ->where('o.orderid','=',$orderid)->first($field);
        if(!empty($data)){
            $data = object_array($data);
            return $data;
        }else{
            return false;
        }
       

    }


    //获取上级 上上级 和 省市区的情况
    private function get_tree_member($id){
        $id = explode(',',$id);
        array_pop($id);
        if(!$id){
           return false;
        }
        $field = ['p.member_id','p.uex_level','p.uex_agent','u.credit1','u.credit2','u.credit3','u.cxhname','u.member_uuid'];
        $data = DB::connection('jmft_cxh')->table('ims_ewei_shop_member as u')
        ->join('ims_cxh_user_ex as p', 'u.id', '=', 'p.member_id')
        ->whereIn('u.id',$id)->get($field);
        if(!empty($data)){
            $data = $data->toArray();
            $data = object_array($data);
            return $data;
        }else{
            return false;
        }
    }


   
   
    
   
   
   

}